<?php
class PotholesController extends AppController {

	var $name = 'Potholes';
	var $helpers = array('Form');
	var $uses = array('Pothole', 'Metro', 'Comment', 'Rating'/*, 'Town', 'User'*/);
	// Cookies:
	function beforeFilter() {
		$this->cookieMonster();
	}
	
	function index()
		{
			 $town = $this->Cookie->read('town.id');
			 $this->set('potholes', $this->Pothole->getPotholes($town, 'created', 'DESC'));
		}

	function add()
		{
		$town = $this->Cookie->read('town.id');
		$metro = $this->Cookie->read('metro.id');
		if (empty($town)) {
			$this->flash('Please select a town before reporting a pothole.','/towns/');
		}
		$this->Town->ID = $town;
		$this->set('town_info', $this->Town->find('first', array('conditions' => array('Town.ID' => $town))));
		$this->Metro->ID = $metro;
		$this->set('metro_info', $this->Metro->find('first', array('conditions' => array('Metro.ID' => $metro))));
		if (!empty($this->data['Pothole']))
			{
			$this->data['Pothole']['ID'] = $this->Uuid->generate();
			// When I get the user login figured out: $this->data['user_ID'] = $this->Cookie->read('User.id');
				if($this->Pothole->save($this->data['Pothole']))
					{
						$townPotholeCount = $this->Pothole->countPotholes($town);
						$this->Pothole->Town->save(array('count_potholes' => $townPotholeCount));
						$this->flash('Your Pothole has been updated.','/potholes/');
					}
			}
		print_r($this->Cookie->read());
		}
		
	function view($id) {
			$this->Pothole->ID = $id;
			$this->set('data', $this->Pothole->fetchPothole($id));
			$this->set('comments', $this->Comment->getComments($id));
			$this->set('rating', $this->Rating->getRating($id));
			$this->set('id', $id);
			$this->set('cookie_data', $this->Cookie->read());
		}

}
?>
